package router

import (
	"blog_server/global"
	"blog_server/internal/controller"
	"blog_server/internal/middleware"
	"github.com/gin-gonic/gin"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
)

func NewRouter() *gin.Engine {
	//controller定义
	articleController := controller.NewArticleController()
	authorController := controller.NewAuthorController()
	tagController := controller.NewTagController()
	categoryController := controller.NewCategoryController()
	LinkController := controller.NewFriendLinkController()
	userController := controller.NewUserController()
	fileController := controller.NewFileController()
	gin.SetMode(global.ServerSetting.RunMode)
	r := gin.New()
	if global.ServerSetting.RunMode == "debug" {
		r.Use(gin.Logger())
	} else {
		gin.SetMode(gin.ReleaseMode)
		r.Use(middleware.AccessLog())
	}
	r.Use(gin.Recovery()).Use(middleware.Cors())

	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	v1 := r.Group("/blogApi/v1/")
	{
		v1.POST("/user/login", userController.Login)
		v1.GET("/articles", articleController.GetArticleList)
		v1.GET("/articles/first", articleController.GetFirstPageArticle)
		v1.GET("/articles/query/:search_text", articleController.GetArticleListBySearchText)
		v1.GET("/articles/:id", articleController.GetArticleByID)
		v1.GET("/articles/tags/amount", tagController.GetArticleTagAmount)
		v1.GET("/articles/editPage/:id", articleController.GetEditPgeArticleById)
		v1.GET("/articles/category/:category_id", articleController.GetArticleByCategoryID)
		v1.GET("/author", authorController.Get)
		v1.GET("/links", LinkController.GetByPagination)
		v1.GET("/links/query/:search_text", LinkController.GetBySearchText)
		v1.GET("/tags/query/:search_text", tagController.GetBySearchText)
		v1.GET("/categories/all", categoryController.GetAll)
		v1.GET("/categories", categoryController.GetByPagination)
		v1.GET("/categories/query/:search_text", categoryController.GetBySearchText)
		v1.GET("/categoriesAndTags", categoryController.GetCategoriesAndTags)
		v1.GET("/user/info", userController.Info)
		v1.GET("/tags", tagController.GetByPagination)
		v1.GET("/imgList", fileController.GetImgList)
		v1.POST("/user/logout", userController.Logout)

		v1.Use(middleware.Auth())
		{
			v1.POST("/articles", articleController.Create)
			v1.PUT("/articles/:id", articleController.UpdateArticle)
			v1.DELETE("/articles/:id", articleController.DeleteArticle)
			v1.POST("/links", LinkController.Create)
			v1.PUT("/links/:id", LinkController.Update)
			v1.DELETE("/links/:id", LinkController.Delete)
			v1.POST("/tags", tagController.Create)
			v1.PUT("/tags/:id", tagController.Update)
			v1.DELETE("/tags/:id", tagController.Delete)
			v1.POST("/categories", categoryController.Create)
			v1.PUT("/categories/:id", categoryController.Update)
			v1.DELETE("/categories/:id", categoryController.Delete)
			v1.POST("/imgUpload", fileController.UploadFile)
			v1.DELETE("/img/:fileName", fileController.DelImg)
		}
	}

	return r
}
